package com.nice.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.nice.Commons;
import com.nice.cache.CacheMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
public class PriceSettingController {

    @Autowired
    private CacheMap cacheMap;
    /**
     * 跳转前端首页
     * @param session
     * @return
     */
    @PostMapping("/priceSetting")
    @ResponseBody
    public String priceSetting(HttpSession session,String name,String email,String down_price,String up_price,String down_change_margin,String up_change_margin) {
        if(StringUtils.isEmpty(name)||
                StringUtils.isEmpty(email)||
                StringUtils.isEmpty(down_price)||
                StringUtils.isEmpty(up_price)||
                StringUtils.isEmpty(down_change_margin)||
                StringUtils.isEmpty(up_change_margin)){
            return "paramNull";
        }
        float f_down_change_margin = getFloat(down_change_margin);//涨跌幅
        float f_up_change_margin = getFloat(up_change_margin);//涨跌幅
        if (f_down_change_margin > 0 ||!down_change_margin.contains("%")) {
            return "down_change_margin_fail";
        }
        if (f_up_change_margin <0 ||!up_change_margin.contains("%")) {
            return "up_change_margin_fail";
        }

        System.out.println(name);
        System.out.println(email);
        System.out.println(down_price);
        System.out.println(up_price);
        System.out.println(down_change_margin);
        System.out.println(up_change_margin);
        //用于组装价格 key=白银,value =白银明细
        Map<String, HashMap> priceMap = new HashMap<String, HashMap>();
        //用于组装几个明细
        Map<String, String> priceDetailMap = new HashMap<String, String>();
        priceDetailMap.put("down_price",down_price);
        priceDetailMap.put("up_price",up_price);
        priceDetailMap.put("down_change_margin",down_change_margin);
        priceDetailMap.put("up_change_margin",up_change_margin);
        //组装priceMap
        priceMap.put(name, (HashMap) priceDetailMap);

        //获取缓存设置，用户已设置的货币或者未设置，存在零个或者多个
        Map<String, HashMap> userCacheMap = (Map<String, HashMap>) cacheMap.getUserPriceSetMap(email);
        if (userCacheMap != null) {
            Set<String>  set = userCacheMap.keySet();
            //遍历用户已设置的报价
            for (String key : set) {
                //取和这次设置不一样的历史设置值
                if (key != null && !"".equals(key)&&!name.equals(key)) {
                    Map<String, HashMap> temp = userCacheMap.get(key);
                    //重新覆盖缓存
                    priceMap.put(key, (HashMap) temp);
                }
            }
        }
        System.out.println("用户设置提醒:"+JSON.toJSONString(priceMap));
        //设置缓存
        cacheMap.putUserPriceSetMap(email,priceMap);

        return Commons.SUCCESS;
    }

    public float getFloat(String value) {
        String[] str1= value.split("%");
        String str2 = str1[0];
        float a = Float.valueOf(str2);
        return a;
    }
    /**
     * post 方法获取白银价格
     * @return
     */
    @PostMapping("/getUserSetPrice")
    @ResponseBody
    public JSONArray getUserSetPrice( HttpSession session) {
        System.out.println("收到请求:getUserSetPrice");
        String email = String.valueOf(session.getAttribute(Commons.LOGIN_NAME));
        List< Map<String, String>> returnlist  =new ArrayList<Map<String, String>>();
        if (email != null && !"".equals(email)) {
            Map<String, HashMap> map = (Map<String, HashMap>) cacheMap.getUserPriceSetMap(email);
            if (map != null && map.keySet().size() > 0) {
                for (String priceName : map.keySet()) {
                    Map<String, String> priceMap =map.get(priceName);
                    Map<String, String> returnMap = new HashMap<String,String>();
                    returnMap.put("name", priceName);
                    returnMap.put("down_price", priceMap.get("down_price"));
                    returnMap.put("up_price", priceMap.get("up_price"));
                    returnMap.put("down_change_margin", priceMap.get("down_change_margin"));
                    returnMap.put("up_change_margin", priceMap.get("up_change_margin"));
                    returnlist.add(returnMap);
                }
            }
        }
        System.out.println("返回前端："+JSON.toJSONString(returnlist));
        return JSON.parseArray(JSON.toJSONString(returnlist));
    }
}
